/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangePassForm.java
 *
 * Created on Jan 9, 2012, 4:29:50 PM
 */
package GUI;

import Model.UserModel;
import Entity.ConnectionSQL;
import Entity.User;
import java.awt.Color;
import java.awt.Toolkit;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

/**
 *
 * @author BigBK
 */
public class DialogChangePass extends javax.swing.JDialog {
    private UserModel usermodel;
    ResourceBundle messages;
    private User user;
    boolean checkPassOld = true;
    boolean checkPassNew = true;
    
    /** Creates new form ChangePassForm */
    public DialogChangePass(java.awt.Frame parent, boolean modal, User user,ResourceBundle messages) {
        super(parent, modal);
        initComponents();
        this.messages = messages;
        //this.setIconImage(Toolkit.getDefaultToolkit().getImage(message.getString("changpassiconurl")));
        this.user = user;
        this.setLocationRelativeTo(null);
        this.lblHello.setVisible(false);
    }
    public DialogChangePass(java.awt.Dialog parent, boolean modal, User user,ResourceBundle message){
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
        this.user = user;
        this.messages = message;
    }

    public void checkPassword(JPasswordField passwordField){
        char[] oldPass = passwordField.getPassword();
        ConnectionSQL conn= new ConnectionSQL(messages);
        String encrypOldPass = conn.EncryptPassword(new String(oldPass));
        if(!encrypOldPass.equals(user.getPassword())){
            passwordField.setBackground(Color.pink);
            checkPassOld = false;
        }
        else{
            checkPassOld = true;
            passwordField.setBackground(Color.white);
        }
    }
    
    public String comparePassword(JPasswordField newPasswordField, JPasswordField confirmPasswordField){
        char[] newPass = newPasswordField.getPassword();
        char[] confirmPass = confirmPasswordField.getPassword();
        String newPassString = new String(newPass);
        if(newPassString.compareTo(new String(confirmPass)) != 0){
            newPasswordField.setBackground(Color.pink);
            confirmPasswordField.setBackground(Color.pink);
            checkPassNew = false;
            return null;
        }
        else{
            checkPassNew = true;
            newPasswordField.setBackground(Color.white);
            confirmPasswordField.setBackground(Color.white);
            return newPassString;
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblHello = new javax.swing.JLabel();
        lblOldPass = new javax.swing.JLabel();
        lblNewPass = new javax.swing.JLabel();
        pfOldPass = new javax.swing.JPasswordField();
        pfNewPass = new javax.swing.JPasswordField();
        lblConfirmPass = new javax.swing.JLabel();
        pfConfirmPass = new javax.swing.JPasswordField();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Change User Password");
        setIconImage(null);

        lblHello.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblHello.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblHello.setText("Change Password");

        lblOldPass.setText("Old Password");

        lblNewPass.setText("New Password");

        pfOldPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pfOldPassActionPerformed(evt);
            }
        });

        lblConfirmPass.setText("Confirm New Password");

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(115, Short.MAX_VALUE)
                .addComponent(btnOK)
                .addGap(18, 18, 18)
                .addComponent(btnCancel)
                .addGap(141, 141, 141))
            .addGroup(layout.createSequentialGroup()
                .addGap(142, 142, 142)
                .addComponent(lblHello)
                .addContainerGap(148, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblConfirmPass)
                    .addComponent(lblNewPass)
                    .addComponent(lblOldPass))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pfOldPass, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pfNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pfConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(85, 85, 85))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblHello)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblOldPass)
                    .addComponent(pfOldPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNewPass)
                    .addComponent(pfNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblConfirmPass)
                    .addComponent(pfConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void pfOldPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pfOldPassActionPerformed
        
    }//GEN-LAST:event_pfOldPassActionPerformed

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        checkPassword(this.pfOldPass);
        if(pfNewPass.getPassword().length<6|| pfNewPass.getPassword().length>30){
            JOptionPane.showMessageDialog(this, messages.getString("messagesInvalidPassword"));
        }else{
        String newPass = comparePassword(this.pfNewPass, this.pfConfirmPass);
        if(checkPassOld && checkPassNew){
            int i = JOptionPane.showConfirmDialog(this, messages.getString("messagesConfirmChangePassword"), messages.getString("titleConfirmChangePassword"), JOptionPane.YES_NO_OPTION);
            if(i == JOptionPane.OK_OPTION){
            ConnectionSQL conn = new ConnectionSQL(messages);
            conn.ChangePassword(user.getId(), newPass);
            JOptionPane.showMessageDialog(this, messages.getString("messagesChangePasswordComplete"));
            this.dispose();
        }
        }
        }
    }//GEN-LAST:event_btnOKActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel lblConfirmPass;
    private javax.swing.JLabel lblHello;
    private javax.swing.JLabel lblNewPass;
    private javax.swing.JLabel lblOldPass;
    private javax.swing.JPasswordField pfConfirmPass;
    private javax.swing.JPasswordField pfNewPass;
    private javax.swing.JPasswordField pfOldPass;
    // End of variables declaration//GEN-END:variables
}
